package torpedo_client;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import torpedo_client.guicomponents.BattleSquare;
import torpedo_client.guicomponents.BattleZone;

public class Gui extends JFrame {

    private Menu menu = new Menu();
    private JPanel buttonsp = new JPanel();
    private JPanel enemyPanel = new JPanel();
    private JPanel myPanel = new JPanel();
    private JPanel centerPanel = new JPanel();
    private JPanel southPanel = new JPanel();
    private JPanel statusPanel = new JPanel();
    public static JLabel shipStatus = new JLabel("Set your ships to your field!");
    public JButton mShip = new JButton("Mother ship");
    public JButton bShip = new JButton("Big ship");
    public JButton sShip = new JButton("Small ship");
    public JButton meShip = new JButton("Medium ship");
    public static int selectShip = 0;
    public static MotherShip newMoShip;
    public static BigShip newBiShip;
    public static MedShip newMedShip;
    public static SmallShip newSmShip;

    public Gui() {
        super("Torpedo");
        setBounds(000, 000, 1200, 900);

        setResizable(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        setVisible(true);
        ItemHandler itemHandler = new ItemHandler();
        statusPanel.setBorder(BorderFactory.createTitledBorder("Instructions"));
        buttonsp.setBorder(BorderFactory.createTitledBorder("Choose a ship to add the field"));
        buttonsp.add(mShip);
        buttonsp.add(bShip);
        buttonsp.add(meShip);
        buttonsp.add(sShip);
        add(centerPanel, "Center");
        centerPanel.setLayout(new GridLayout(1, 2));
        centerPanel.add(myPanel);
        centerPanel.add(enemyPanel);
        add(menu, "North");
        southPanel.add(buttonsp, "West");
        statusPanel.add(shipStatus, "Center");
        southPanel.add(statusPanel, "Center");
        add(southPanel, "South");
        defineTheField(myPanel, enemyPanel);
        mShip.addActionListener(itemHandler);
        meShip.addActionListener(itemHandler);
        bShip.addActionListener(itemHandler);
        sShip.addActionListener(itemHandler);
        invalidate();
        this.repaint();
    }

    private void defineTheField(JPanel my, JPanel enemy) {
        my.setLayout(new BorderLayout());
        enemy.setLayout(new BorderLayout());
        my.add(new BattleZone(false, 8, "Our Field"));
        enemy.add(new BattleZone(true, 8, "Enemy Field"));
    }

    public static int getSelectedShip() {
        return selectShip;
    }

    private class ItemHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == mShip) {
                mShip.setEnabled(false);
                selectShip = 4;
                newMoShip = new MotherShip(BattleSquare.positionlist);
            }
            if (e.getSource() == bShip) {
                bShip.setEnabled(false);
                selectShip = 3;
                newBiShip = new BigShip(BattleSquare.positionlist);
            }
            if (e.getSource() == meShip) {
                meShip.setEnabled(false);
                selectShip = 2;
                newMedShip = new MedShip(BattleSquare.positionlist);
            }
            if (e.getSource() == sShip) {
                sShip.setEnabled(false);
                selectShip = 1;
                newSmShip = new SmallShip(BattleSquare.positionlist);
            }
        }
    }
}